﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;
using msBsc.Cls;
using msBsc.AppLogic.DataAlc.cls;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 地接客行驶证
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-8-31 13:16
    public class CBizXingsiLicense : CustBizObject
    {
        private CDmDjkXingsiLicense m_CDmDjkXingsiLicense = new CDmDjkXingsiLicense();
        private readonly List<CDmDjkXingsiLicense> _allAreaInfoList = null;
        private CDmDjkXingsiLicense oDmDjkXingsiLicense
        {
            get { return m_CDmDjkXingsiLicense; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizXingsiLicense()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkXingsiLicense.DJK_XINGSI_LICENSE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["XINGSI_LICENSE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {

            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加行驶证
        /// </summary>
        /// <param name="djackId">地接客id</param>
        /// <param name="vehicleNumber">车牌号</param>
        /// <param name="plyPicture">行驶证图片</param>
        /// <param name="vehicleType">车辆类型</param>
        /// <param name="homePerson">所属人</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地Id</param>
        /// <param name="destinationName">目的地名称</param>
        /// <param name="property">使用性质</param>
        /// <param name="brandModel">品牌型号</param>
        /// <param name="vehicleCode">车辆识别码</param>
        /// <param name="engineCode">发动机识别码</param>
        /// <param name="registerDate">注册时间</param>
        /// <param name="issuingDate">发证日期</param>
        /// <param name="publisherId">添加人</param>
        /// <returns></returns>
        ///  Author:任延华
        ///  Creation date:2014-9-7
        public string AddXingsiLicense(string djackId, string vehicleNumber, string plyPicture, string vehicleType, string homePerson, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string property, string brandModel, string vehicleCode, string engineCode, string registerDate, string issuingDate, string publisherId)
        {
            #region 数据验证
            if (string.IsNullOrEmpty(djackId) || string.IsNullOrEmpty(vehicleNumber) || string.IsNullOrEmpty(plyPicture) || string.IsNullOrEmpty(vehicleType) || string.IsNullOrEmpty(homePerson) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(property) || string.IsNullOrEmpty(brandModel) || string.IsNullOrEmpty(registerDate) || string.IsNullOrEmpty(issuingDate))
            {
                return "请完善行驶证信息！";
            }

            if (vehicleNumber.Length > 10)
            {
                return "车牌号最大长度不得超过10个字符";
            }
            if (homePerson.Length > 50)
            {
                return "所属人不得超过50个字符";
            }
            if (property.Length > 36)
            {
                return "使用性质不得超过36个字符";
            }
            if (brandModel.Length > 50)
            {
                return "品牌型号不得超过50个字符";
            }
            if (vehicleCode.Length > 36)
            {
                return "车辆代码不得超过36个字符";
            }
            if (engineCode.Length > 12)
            {
                return "发动机号不得超过12个字符";
            }
            if (!CommValiCollection.IsDate(registerDate))
            {
                return "注册日期格式不正确";
            }

            if (!CommValiCollection.IsDate(issuingDate))
            {
                return "发证日期格式不正确";
            }
            int vType = 1;
            int.TryParse(vehicleType, out vType);
            #endregion

            #region 添加车辆
            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                DataSet ds = new CDmDjkXingsiLicense();
                DataRow dr = ds.Tables[0].NewRow();
                dr["XINGSI_LICENSE_ID"] = this.DataObj.NewID();
                dr["PLATE_NUMBER"] = vehicleNumber;
                dr["VEHICLE_TYPE"] = vType;
                dr["ALL_PEOPLE"] = homePerson;
                dr["COUNTRY_ID"] = countryId;
                dr["COUNTRY_NAME"] = countryName;
                dr["CITY_ID"] = cityId;
                dr["CITY_NAME"] = cityName;
                dr["AREA_ID"] = destinationId;
                dr["AREA_NAME"] = destinationName;
                dr["USER_PROPERTIES"] = property;
                dr["BRAND_MODEL"] = brandModel;
                dr["VEHICLE_IDENTIFCATION_CODE"] = vehicleCode;
                dr["ENGINE_NUMBER"] = engineCode;
                dr["REGISTRATION_DATE"] = registerDate;
                dr["ISSUING_DATE"] = issuingDate;
                dr["CREATE_USERID"] = publisherId;
                dr["CREATE_DATE"] = this.dtCurrDateTime;
                dr["BELONGS_USERID"] = djackId;
                dr["IS_DELETE"] = 0;
                dr["DOCUMENT_IMAGE"] = plyPicture;
                ds.Tables[0].Rows.Add(dr);
                this.Update(ds);
                using (CBizCustomerBaseInfo baseInfo = new CBizCustomerBaseInfo())
                {
                    string[] certie = new string[] { "IS_XINGSI_LICENSE" };
                    string ret = baseInfo.UpdateCertificatesStatus(certie, djackId, true);
                    if (ret != "true")
                    {
                        return "更新用户标识出错";
                    }
                }
                ts.Complete();
                return "OK";
            }
            #endregion
        }

        /// <summary>
        /// 修改行驶证
        /// </summary>
        /// <param name="licenseID">行驶证id</param>
        /// <param name="vehicleNumber">车牌号</param>
        /// <param name="plyPicture">行驶证图片</param>
        /// <param name="vehicleType">车辆类型</param>
        /// <param name="homePerson">所属人</param>
        /// <param name="countryId">国家Id</param>
        /// <param name="countryName">国家名称</param>
        /// <param name="cityId">城市Id</param>
        /// <param name="cityName">城市名称</param>
        /// <param name="destinationId">目的地Id</param>
        /// <param name="destinationName">目的地名称</param>
        /// <param name="property">使用性质</param>
        /// <param name="brandModel">品牌型号</param>
        /// <param name="vehicleCode">车辆识别码</param>
        /// <param name="engineCode">发动机识别码</param>
        /// <param name="registerDate">注册时间</param>
        /// <param name="issuingDate">发证日期</param>
        /// <param name="publisherId">添加人</param>
        /// <returns></returns>
        ///  Author:蒲崇彬
        ///  Creation date:2015-9-14
        public string UpdateXingsiLicense(string licenseID, string vehicleNumber, string plyPicture, string vehicleType, string homePerson, string countryId, string countryName, string cityId, string cityName, string destinationId, string destinationName, string property, string brandModel, string vehicleCode, string engineCode, string registerDate, string issuingDate, string publisherId)
        {
            #region 数据验证
            if (string.IsNullOrEmpty(vehicleNumber) || string.IsNullOrEmpty(plyPicture) || string.IsNullOrEmpty(vehicleType) || string.IsNullOrEmpty(homePerson) || string.IsNullOrEmpty(countryId) || string.IsNullOrEmpty(countryName) || string.IsNullOrEmpty(cityId) || string.IsNullOrEmpty(cityName) || string.IsNullOrEmpty(destinationId) || string.IsNullOrEmpty(destinationName) || string.IsNullOrEmpty(property) || string.IsNullOrEmpty(brandModel) || string.IsNullOrEmpty(registerDate) || string.IsNullOrEmpty(issuingDate))
            {
                return "请完善行驶证信息！";
            }

            if (vehicleNumber.Length > 10)
            {
                return "车牌号最大长度不得超过10个字符";
            }
            if (homePerson.Length > 50)
            {
                return "所属人不得超过50个字符";
            }
            if (property.Length > 36)
            {
                return "使用性质不得超过36个字符";
            }
            if (brandModel.Length > 50)
            {
                return "品牌型号不得超过50个字符";
            }
            if (vehicleCode.Length > 36)
            {
                return "车辆代码不得超过36个字符";
            }
            if (engineCode.Length > 12)
            {
                return "发动机号不得超过12个字符";
            }
            if (!CommValiCollection.IsDate(registerDate))
            {
                return "注册日期格式不正确";
            }

            if (!CommValiCollection.IsDate(issuingDate))
            {
                return "发证日期格式不正确";
            }
            int vType = 1;
            int.TryParse(vehicleType, out vType);
            #endregion

            DataSet ds = GetXinsiByID(licenseID);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return "证件信息不存在";
            }

            DataRow dr = ds.Tables[0].Rows[0];
            dr["PLATE_NUMBER"] = vehicleNumber;
            dr["VEHICLE_TYPE"] = vType;
            dr["ALL_PEOPLE"] = homePerson;
            dr["COUNTRY_ID"] = countryId;
            dr["COUNTRY_NAME"] = countryName;
            dr["CITY_ID"] = cityId;
            dr["CITY_NAME"] = cityName;
            dr["AREA_ID"] = destinationId;
            dr["AREA_NAME"] = destinationName;
            dr["USER_PROPERTIES"] = property;
            dr["BRAND_MODEL"] = brandModel;
            dr["VEHICLE_IDENTIFCATION_CODE"] = vehicleCode;
            dr["ENGINE_NUMBER"] = engineCode;
            dr["REGISTRATION_DATE"] = registerDate;
            dr["ISSUING_DATE"] = issuingDate;
            dr["DOCUMENT_IMAGE"] = plyPicture;
            ds.Tables[0].TableName = this.TableName;
            this.Update(ds);
            return "OK";
        }

        /// <summary>
        /// 根据行驶证id或许详情
        /// </summary>
        /// <param name="licenseID">行驶证id</param>
        /// <returns>DataSet</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-14 9:35
        public DataSet GetXinsiByID(string licenseID)
        {
            if (string.IsNullOrEmpty(licenseID))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("licenseID", ParameterDirection.Input, EDbType.String, 36, licenseID));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT [XINGSI_LICENSE_ID],[PLATE_NUMBER],[VEHICLE_TYPE],[ALL_PEOPLE],[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[USER_PROPERTIES],[BRAND_MODEL],[VEHICLE_IDENTIFCATION_CODE],[ENGINE_NUMBER],[REGISTRATION_DATE],[ISSUING_DATE],[BELONGS_USERID],[DOCUMENT_IMAGE] FROM [dbo].[DJK_XINGSI_LICENSE](NOLOCK)  WHERE [XINGSI_LICENSE_ID]=@licenseID", parmCollection);
            ds.Tables[0].Columns.Add("VEHICLE_TYPE_NAME", typeof(string));

            for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                ds.Tables[0].Rows[i]["VEHICLE_TYPE_NAME"] = Enum.GetName(typeof(CommonEnum.VehicleTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_TYPE"]));

            }
            return ds;
        }

        /// <summary>
        /// 根据行驶证id或许详情
        /// </summary>
        /// <param name="licenseID">行驶证id</param>
        /// <returns>DataSet</returns>
        /// Author:任延华
        /// Creation date:2015-9-24
        public DataSet GetXinsiDjackID(string djackId)
        {
            if (string.IsNullOrEmpty(djackId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("djackId", ParameterDirection.Input, EDbType.String, 36, djackId));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT [XINGSI_LICENSE_ID],[PLATE_NUMBER],[VEHICLE_TYPE],[ALL_PEOPLE],[COUNTRY_ID],[COUNTRY_NAME],[CITY_ID],[CITY_NAME],[AREA_ID],[AREA_NAME],[USER_PROPERTIES],[BRAND_MODEL],[VEHICLE_IDENTIFCATION_CODE],[ENGINE_NUMBER],[REGISTRATION_DATE],[ISSUING_DATE],[BELONGS_USERID],[DOCUMENT_IMAGE] FROM [dbo].[DJK_XINGSI_LICENSE](NOLOCK)  WHERE [BELONGS_USERID]=@djackId AND [IS_DELETE]=0", parmCollection);
            ds.Tables[0].Columns.Add("VEHICLE_TYPE_NAME", typeof(string));

            for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                ds.Tables[0].Rows[i]["VEHICLE_TYPE_NAME"] = Enum.GetName(typeof(CommonEnum.VehicleTypeEnum), Convert.ToInt32(ds.Tables[0].Rows[i]["VEHICLE_TYPE"]));

            }
            return ds;
        }
    }
}
